package com.yzg.timing;

import lombok.extern.slf4j.Slf4j;

import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * @author misterWei
 * @create 2020年06月14号:18点50分
 * @mailbox forcontinue@163.com
 *
 * 任务 -- 每周四下午18点执行
 */
@Slf4j(topic = "c.ScheduledBusMain")
public class ScheduledBusMain {
    public static void main(String[] args) {
        ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
        // 计算从本周四开始 18
        LocalDateTime now = LocalDateTime.now();
        // 14
        LocalDateTime THURSDAYexe = now.with(DayOfWeek.THURSDAY).withHour(18).withMinute(0).withSecond(0).withNano(0);
        // 如果当前时间已经超过 本周四 18:00:00.000， 那么找下周四 18:00:00.000
        if (now.compareTo(THURSDAYexe) > 0){
            //次方法由于本人严重模糊,需要好好捋捋
            /**
             * compareTo 在时间中APi 的使用就是 如果 now的时间在 THURSDAYexe 之前的时候 返回 1
             *
             * */
          THURSDAYexe =   THURSDAYexe.plusWeeks(1);
        }
        //计算时间差值
        long initialDelay =  Duration.between(now,THURSDAYexe).toMillis();
        // 计算间隔一周的执行间隔
        long delay = 7 * 24 * 3600 * 1000;
        service.scheduleWithFixedDelay(()->{
            log.info("执行时间{}", now);
            },
                initialDelay,delay, TimeUnit.MILLISECONDS);

    }
}
